package com.moss.system.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.moss.common.constant.Constants;
import com.moss.common.domain.LoginUser;
import com.moss.common.domain.TreeEntity;
import com.moss.common.entity.SysMenuEntity;
import com.moss.common.exception.ServiceException;
import com.moss.common.util.IdUtil;
import com.moss.common.util.PinYin4JCn;
import com.moss.common.entity.SysDeptEntity;
import com.moss.system.mapper.DeptMapper;
import com.moss.system.service.DeptService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class DeptServiceImpl implements DeptService {

    @Resource
    DeptMapper deptMapper;


    @Override
    public List<SysDeptEntity> selectList(SysDeptEntity dept) {
        return buildTree(deptMapper.selectList(dept));
    }

    private List<SysDeptEntity> buildTree(List<SysDeptEntity> deptList) {
        Set<Long> ids = deptList.stream()
                .map(SysDeptEntity::getDeptId)
                .collect(Collectors.toSet());

        return deptList.stream()
                .filter(m -> !ids.contains(m.getParentId()))
                .peek(m -> m.setChildren(getChildrenList(m, deptList)))
                .collect(Collectors.toList());
    }

    private List<SysDeptEntity> getChildrenList(SysDeptEntity root, List<SysDeptEntity> all) {
        return all.stream().filter(m -> Objects.equals(m.getParentId(), root.getDeptId())).peek(
                (m) -> m.setChildren(getChildrenList(m, all))
        ).collect(Collectors.toList());
    }

    @Override
    public SysDeptEntity selectById(Long deptId) {
        return deptMapper.selectById(deptId);
    }


    @Override
    public void saveDept(SysDeptEntity dept) {
        checkParam(dept,true);
        deptMapper.insert(dept);
    }

    private void checkParam(SysDeptEntity dept, boolean save) {
        Long parentId = dept.getParentId();
        if (parentId == null){
            throw new ServiceException("请选择上级部门");
        }
        SysDeptEntity parentDept = deptMapper.selectById(parentId);
        dept.setAncestors(parentDept.getAncestors() + "," + parentId);
        if (parentDept.getStatus() == 0){
            throw new ServiceException("部门停用，不允许操作");
        }
        String deptName = dept.getDeptName();
        if (StringUtils.isEmpty(deptName)){
            throw new ServiceException("请填写部门名称");
        }
        Integer orderNum = dept.getOrderNum();
        if (orderNum == null){
            throw new ServiceException("请填写显示顺序");
        }
        SysDeptEntity deptByName = deptMapper.selectDeptByName(deptName,parentId);
        if (save && deptByName != null){
            throw new ServiceException("当前部门已存在");
        }
        if (!save && deptByName != null && !deptByName.getDeptId().equals(dept.getDeptId())){
            throw new ServiceException("当前部门已存在");
        }
    }

    @Override
    public void updateDept(SysDeptEntity dept) {
        checkParam(dept,false);
        deptMapper.updateById(dept);
    }

    @Override
    public void deleteDept(SysDeptEntity dept) {
        if (deptMapper.checkHasChild(dept)){
            throw new ServiceException("存在下级部门，不允许删除");
        }
        if (deptMapper.checkHasUser(dept)){
            throw new ServiceException("部门下存在用户，不允许删除");
        }
        deptMapper.deleteById(dept);
    }


    @Override
    public List<TreeEntity> getDeptTree(SysDeptEntity dept) {
        List<SysDeptEntity> list = deptMapper.selectList(dept);
        List<SysDeptEntity> deptTrees = buildTree(list);
        return deptTrees.stream().map(TreeEntity::new).collect(Collectors.toList());
    }
}
